2024-09-30 振り返り
| 2024-09-30への引き継ぎ事項
1. 2024-09-30にやることを視界に入れる
2. 重複したタスクをずらす
↑2024-09-29に書く
↓2024-09-30に書く
check:
1. あさごはんを食べる
| 今日やったこと
20:46:40 ひらめいた
code:mod.ts
interface Reader {
read: (input: never) => unknown;
}
type Input<R extends Reader> = Parameters<R"read">0; interface Parser<A> {
reader?: Reader;
parse: (reader: NonNullable<this"reader">, input: Input<NonNullable<this"reader">>) => ParseOutput<A, NonNullable<this"reader">>; }
type Parse<A, ParserFn extends Parser<A>, R extends NonNullable<ParserFn"reader">> = (Parser<A> & { reader?: R })"parse"; type ParseOutput<A, R extends Reader> = ParseOk<A, R> | ParseErr;
interface ParseOk<A, R extends Reader> {
ok: true;
value: A;
rest: Input<R>;
}
interface ParseErr {
ok: false;
}
const chain = <A, B>(
parser: Parser<A>,
fn: (a: A) => Parser<B>,
): Parser<B> => ({
parse: (reader, input) => {
const result = parser.parse(reader, input);
if(!result.ok) return result;
return fn(result.value).parse(reader, result.rest);
},
});
declare const ok: <A>(a: A) => Parser<A>;
const map = <A, B>(
parser: Parser<A>,
fn: (a: A) => B,
): Parser<B> => chain(parser, (a) => ok(fn(a)));
const and = <A, B>(
parserA: Parser<A>,
parserB: Parser<B>,
): Parser<A, B> => chain(parserA, (a) => map(parserB, (b) => a, b)); interface TextReader extends Reader {
read: (input: string) => string;
}
declare const textReader: TextReader;
interface ByteReader extends Reader {
read: (input: string) => string;
}
declare const byteReader: ByteReader;
declare const aa: Parser<"aa">;
declare const bb: Parser<"bb">;
const test = and(aa, bb);
// ^?
const parse = <A, R extends Reader>(reader: R, parser: Parser<A>): (input: Input<R>) => ParseOutput<A, R> => (input) => (parser.parse as Parse<A, Parser<A>, R>)(reader, input);
ちょっとうまくいかない